'use strict';

/**
 * phonemap.util.js
 * Author: Phu
 * Date: May 28, 2014
 */

/**
 * Constructor.
 */
function PhonemapUtil() {
	
}

/**
 * get avatar
 * @param user
 * @returns {String}
 */
PhonemapUtil.getAvatar = function(user) {
	return 'http://graph.facebook.com/'+user.user_id+'/picture';
} 

/**
 * 
 * @param geoPtString
 * @returns {google.maps.LatLng}
 */
PhonemapUtil.getLatLng = function(geoPtString) {
	var array = geoPtString.split(",");
	return new google.maps.LatLng(array[0], array[1]);
}


/**
 * get Popup info of avatar
 * @param user
 * @returns {String}
 */
PhonemapUtil.getPopupInfo = function(user) {
	var online = PhonemapUtil.isOnline(user.located_time)==true ? 'online':'offline';
	var gender = user.gender != PhonemapConstant.NULL ? user.gender : 'Unknown';
	var info = '<div><ul>'+
					'<li><a href=\''+user.link+'\'  target=\'_blank\'>'+user.fullname+'</a></li>'+
					'<li>'+ gender + '</li>'+
					'<li>'+user.located_time+'</li>'+
					'</ul></div>';
	return info;
} 

/**
 * Checks online
 * @param time
 * @returns {Boolean}
 */
PhonemapUtil.isOnline = function(time) {
	var date = PhonemapUtil.convertDateTime(time);
	var current = new Date();
	var d = Date.parse(current) - Date.parse(date);
	if ((Date.parse(current) - Date.parse(date)) < PhonemapConstant.MAX_TIME)
		return true;
	return false;
}

/**
 * convert date time
 * @param time
 * @returns {Date}
 */
PhonemapUtil.convertDateTime = function(time){
	
	var month_abbrs = {
                       'Jan': '01',
                       'Feb': '02',
                       'Mar': '03',
                       'Apr': '04',
                       'May': '05',
                       'Jun': '06',
                       'Jul': '07',
                       'Aug': '08',
                       'Sep': '09',
                       'Oct': '10',
                       'Nov': '11',
                       'Dec': '12'
					};
    var dateTime = time.split(" ");
    return new Date(dateTime[5]+'-'+month_abbrs[dateTime[1]]+'-'+dateTime[2]+'T'+dateTime[3]);
}

/**
 * Gets Icon
 * @param avatar
 * @returns {___anonymous2007_2227}
 */
PhonemapUtil.getIcon = function(avatar) {
	var sizeX = 100;
	var sizeY = 100;
	var scaledSizeX = 50;
	var scaledSizeY = 50;
	if (PhonemapConstant.SCREEN_WIDTH < 750) {
		var sizeX = 50;
		var sizeY = 50;
		var scaledSizeX = 40;
		var scaledSizeY = 40;
	} 
	
	var icon = {
			url : avatar,
			size : new google.maps.Size(sizeX, sizeY),
			origin : new google.maps.Point(0, 0),
			anchor : new google.maps.Point(17, 34),
			scaledSize : new google.maps.Size(scaledSizeX, scaledSizeY)
		};
	
	return icon;
}

/**
 * Validate text input number.
 * @param evt
 */
PhonemapUtil.validate = function(evt) {
	 var theEvent = evt || window.event;
	var key = theEvent.keyCode || theEvent.which;
	key = String.fromCharCode(key);
	var regex = /[0-9]/;
	if (!regex.test(key)) {
		theEvent.returnValue = false;
		if (theEvent.preventDefault)
			theEvent.preventDefault();
	}
}

/**
 * Distance between 2 points
 * 'M' is statute miles    
 * 'K' is kilometers (default)  
 * 'N' is nautical miles                             
 * @param lat1
 * @param lon1
 * @param lat2
 * @param lon2
 * @param unit
 * @returns {Number}
 */
PhonemapUtil.distance = function(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var radlon1 = Math.PI * lon1/180
    var radlon2 = Math.PI * lon2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist
}

/**
 * Format round number 
 * @param value
 * @returns {String}
 */
PhonemapUtil.formatRound = function(value) {
	if (value < 1)
		return Math.round(value*1000) + 'm';
	else
		return Math.round(value) + 'km';
}

/**
 * Sets Profile.
 * @param user
 */
PhonemapUtil.setProfile = function(user) {
	var avatar = PhonemapUtil.getAvatar(user);
	//set info to avatar
	$('#avatar').attr('src', avatar);
	$('#avatarname').text(user.fullname);
	//$('#avataraccount').text(user.username);
	
	//set profile
	if (user.fullname != '' && user.fullname != 'null') {
//		$('#fullname').val(user.fullname);
		$('#fullname').text(user.fullname);
	}
	if (user.gender != '' && user.gender != 'null') {
		$('#gender').text(user.gender);
	}
	
	if (user.link != '' && user.link != 'null') {
		$('#link').text(user.link);
	}
	
	if (user.birthday != '' && user.birthday != 'null') {
		$('#birthday').text(user.birthday);
	}
	
	if (user.interested_in != '' && user.interested_in != 'null') {
		$('#interested_in').text(user.interested_in);
	}
	
	if (user.relationship_status != '' && user.relationship_status != 'null') {
		$('#relationship_status').text(user.relationship_status);
	}
	
	if (user.email != '' && user.email != 'null') {
		$('#email').text(user.email);
	}
	if (user.phone != '' && user.phone != 'null') {
		$('#phone').text(user.phone);
	}
	
	if (user.username != '' && user.username != 'null') {
		$('#username').text(user.username);
	}
}

/**
 * Calcualte time.
 * @param offset
 * @returns
 */
PhonemapUtil.calculateTime = function(offset) {

    // create Date object for current location
    var d = new Date();

    // convert to msec
    // add local time zone offset
    // get UTC time in msec
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    //nd = new Date(utc + (3600000*offset));

    // return time in long
    return (utc + (3600000*offset))

}

/**
 * Set local storage.
 * @param key
 * @param value
 */
PhonemapUtil.setLocalStorage = function(key, value) {
	localStorage.setItem(key, value);
}

/**
 * Goes home page.
 */
PhonemapUtil.goHome = function() {
	document.location.href = '/login.html';
}


/**
 * Goes home page.
 */
PhonemapUtil.goApp = function(page) {
	//var page = PhonemapGlobal.getCurPage();
	if (page == PhonemapConstant.FOLLOW_PAGE) {
//		document.location.href = '/apps/follows.html';
		PhonemapFollow.goApp();
	} else if (page == PhonemapConstant.SEARCH_PAGE) {
//		document.location.href = '/apps/search.html';
		PhonemapSearch.goApp();
	} else if (page == PhonemapConstant.LOOK_AROUND_PAGE) {
		PhonemapLoookAround.goApp();
	} else {
		PhonemapUtil.goHome();
	}
	
}

var isMobile = {
	    Android: function() {
	        return navigator.userAgent.match(/Android/i);
	    },
	    BlackBerry: function() {
	        return navigator.userAgent.match(/BlackBerry/i);
	    },
	    iOS: function() {
	        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
	    },
	    Opera: function() {
	        return navigator.userAgent.match(/Opera Mini/i);
	    },
	    Windows: function() {
	        return navigator.userAgent.match(/IEMobile/i);
	    },
	    any: function() {
	        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
	    }
	};

/**
 * Is mobile logged
 */
PhonemapUtil.isMobile = function() {
	return isMobile.any();
}

/**
 * Gets userId from cookie
 * @returns userId
 */
PhonemapUtil.getUserId = function() {
	return $.cookie('useraccount');
}


